package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.aCloudBean;


/**
 * Created by XXX on 2017/4/3.
 */
public class ToolListAdapter extends BaseAdapter {
    private Context context;
    private List<aCloudBean> list;

    public ToolListAdapter(Context context, List<aCloudBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.toolbox_info, parent, false);
            holder = new ViewHolder();
            holder.toolTite = (TextView) convertView.findViewById(R.id.toolTitle);
            holder.filesize = (TextView) convertView.findViewById(R.id.filesize);
            holder.folderImg = (ImageView) convertView.findViewById(R.id.folderImg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        aCloudBean bean = list.get(position);
        //设置显示的图标
        if (!bean.getType().equals("file")) {
            holder.folderImg.setImageResource(R.mipmap.icon_folder);
            holder.filesize.setText("");
        } else {
            //根据文件的后缀显示
            if (bean.getExt().equals("docx") || bean.getExt().equals("doc")) {
                holder.folderImg.setImageResource(R.mipmap.icon_word);
            }
            else if (bean.getExt().equals("pdf")) {
                holder.folderImg.setImageResource(R.mipmap.icon_pdf);
            }
            else if (bean.getExt().equals("jpg")||bean.getExt().equals("jpeg")||bean.getExt().equals("png")) {
                holder.folderImg.setImageResource(R.mipmap.icon_jpg);
            }
            else if (bean.getExt().equals("txt")) {
                holder.folderImg.setImageResource(R.mipmap.icon_text);
            }
            else if (bean.getExt().equals("zip")) {
                holder.folderImg.setImageResource(R.mipmap.icon_zip);
            }
            else if(bean.getExt().equals("mp4")){
                holder.folderImg.setImageResource(R.mipmap.icon_move);
            }
            else if (bean.getExt().equals("xls")||bean.getExt().equals("xlsx")){
                holder.folderImg.setImageResource(R.mipmap.icon_xls);
            }
            else if (bean.getExt().equals("ppt")||bean.getExt().equals("pptx")){
                holder.folderImg.setImageResource(R.mipmap.icon_ppt);
            }
            else if (bean.getExt().equals("mp3")||bean.getExt().equals("mav")){
                holder.folderImg.setImageResource(R.mipmap.icon_mp3);
            }
            else
            {
                holder.folderImg.setImageResource(R.mipmap.icon_text);
            }
            holder.filesize.setText(getDataSize(bean.getSize()));
        }

        holder.toolTite.setText(bean.getName());


        return convertView;
    }

    class ViewHolder {
        TextView toolTite;
        TextView filesize;
        ImageView folderImg;
    }

    public static String getDataSize(int size) {
        DecimalFormat formater = new DecimalFormat("####.0");
        if (size < 1024) {
            return size + "bytes";
        } else if (size < 1024 * 1024) {
            float kbsize = size / 1024f;
            return formater.format(kbsize) + "KB";
        } else if (size < 1024 * 1024 * 1024) {
            float mbsize = size / 1024f / 1024f;
            return formater.format(mbsize) + "MB";
        } else if (size < 1024 * 1024 * 1024 * 1024) {
            float gbsize = size / 1024f / 1024f / 1024f;
            return formater.format(gbsize) + "GB";
        } else {
            return "size: error";
        }
    }
}
